package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //向检查组中加入检查组合
        checkGroupDao.add(checkGroup);
        //向中间表中加入检查组和检查项的关联关系
        setCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);

    }

    //向中间表中加入检查组和检查项的关联关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //利用mybatis的分页助手插件进行分页查询
        PageHelper.startPage(currentPage, pageSize);
        //根据条件进行分页查询
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckGroup> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> checkItemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return checkItemIds;
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //编辑检查组(即基本信息)
        checkGroupDao.updateCheckGroup(checkGroup);
        //删除关联检查项信息
        checkGroupDao.deleteCheckGroupAndCheckItem(checkGroup.getId());
        //重新将关联关系加入中间表中
        setCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);
    }


    //删除检查组信息
    @Override
    public void deleteCheckGroup(Integer id) {

        //从套餐和检查组的中间表中查询二者有无关联关系，如果有，则不能删除
        int count = checkGroupDao.findCountByCheckGroupId(id);
        //System.out.println(count);
        if (count > 0) {
            //有关联,不能删除
            throw new RuntimeException("当前检查组有关联，不能删除");
        } else {
            //没有关联，就先删除关联关系
            checkGroupDao.deleteCheckGroupAndCheckItem(id);
            checkGroupDao.deleteCheckGroup(id);
        }

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }


}
